package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.enumutil.AddWechatEnum;
import com.ruoyi.system.enumutil.AuditRecordsEnum;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.AddGroupService;
import com.ruoyi.system.service.AddWechatService;
import com.ruoyi.system.service.AuditRecordsService;
import com.ruoyi.system.service.FreezeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： AuditRecordsController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/AuditRecords")
public class AuditRecordsController {

    @Resource
    private AuditRecordsService auditRecordsService;


    //查询
    @PostMapping("/get/AuditRecords")
    public AjaxResult getAuditRecords(@RequestBody AuditRecords auditRecords){
        //创建分页对象
        Page<AuditRecords> page = new Page<>();
        //设置页码
        page.setCurrent(auditRecords.getCurrent());
        //设置行数
        page.setSize(auditRecords.getSize());
        //执行查询
        auditRecordsService.page(page,new LambdaQueryWrapper<AuditRecords>()
                //用户id
                .eq(Objects.nonNull(auditRecords.getUserId()),AuditRecords::getUserId,auditRecords.getUserId())
                //待审核的项目id
                .eq(Objects.nonNull(auditRecords.getAddWechatId()),AuditRecords::getAddWechatId,auditRecords.getAddWechatId())
                //审核类型
                .eq(Objects.nonNull(auditRecords.getAuditType()),AuditRecords::getAuditType,auditRecords.getAuditType())
                //审核状态
                .eq(Objects.nonNull(auditRecords.getAddStatus()),AuditRecords::getAddStatus,auditRecords.getAddStatus())
                //逻辑删除
                .eq(AuditRecords::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(AuditRecords::getCreateTime)
                //添加分页语句
                .last(" limit " + (auditRecords.getCurrent() - 1) * auditRecords.getSize() +"," + auditRecords.getSize())
        );
        page.setTotal(auditRecordsService.count(new LambdaQueryWrapper<AuditRecords>()
                //用户id
                .eq(Objects.nonNull(auditRecords.getUserId()),AuditRecords::getUserId,auditRecords.getUserId())
                //待审核的项目id
                .eq(Objects.nonNull(auditRecords.getAddWechatId()),AuditRecords::getAddWechatId,auditRecords.getAddWechatId())
                //审核类型
                .eq(Objects.nonNull(auditRecords.getAuditType()),AuditRecords::getAuditType,auditRecords.getAuditType())
                //审核状态
                .eq(Objects.nonNull(auditRecords.getAddStatus()),AuditRecords::getAddStatus,auditRecords.getAddStatus())
                //逻辑删除
                .eq(AuditRecords::getDeleteFlag,Boolean.FALSE)

        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/AuditRecords/{id}")
    public AjaxResult deleteAuditRecords(@PathVariable(value = "id")Long id){
        //调用Service层删除
        auditRecordsService.update(null,new UpdateWrapper<AuditRecords>().lambda()
                //主键id
                .eq(AuditRecords::getId,id)
                //设置逻辑删除为 真
                .set(AuditRecords::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }
    
    //增加
    @PostMapping("/insert/AuditRecords")
    public AjaxResult insertAuditRecords(@RequestBody AuditRecords auditRecords){
        //设置默认值
        auditRecords.setCreateBy(SecurityUtils.getUsername());
        auditRecords.setCreateTime(new Date());
        auditRecords.setUpdateBy(SecurityUtils.getUsername());
        auditRecords.setUpdateTime(new Date());
        auditRecords.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        auditRecordsService.save(auditRecords);
        //返回结果
        return AjaxResult.success("新增成功");
    }
    @Resource
    private SysUserMapper sysUserMapper;
    //修改
    @PostMapping("/update/AuditRecords")
    public AjaxResult updateAuditRecords(@RequestBody AuditRecords auditRecords){

        //设置默认值
        auditRecords.setUpdateTime(new Date());
        auditRecords.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        auditRecordsService.updateById(auditRecords);
        //返回结果
        return AjaxResult.success("修改成功");
    }


    //提交审核
    @PostMapping("/insert/AuditRecords/uniapp")
    public AjaxResult insertAuditRecordsUniapp(@RequestBody AuditRecords auditRecords){
        //查询是否为只允许完成一次任务的人
        SysUser addUser = sysUserMapper.selectUserById(auditRecords.getAddWechatUserId());
        //判断接收任务的人是不是发布者本人
        if (addUser.getUserId() == SecurityUtils.getUserId()){
            //返回提示
            return AjaxResult.error("您是任务发布人,审核提交失败。");
        }
        //判断用户是否为只允许薅羊毛一次
        if (addUser.getPickType() == 1){
            //查询是否已经接收过该任务
            AuditRecords bo = auditRecordsService.getOne(new LambdaQueryWrapper<AuditRecords>()
                    //发布用户id
                    .eq(AuditRecords::getAddWechatUserId, addUser.getUserId())
                    //接收任务用户id
                    .eq(AuditRecords::getUserId, SecurityUtils.getUserId())
                    //逻辑删除
                    .eq(AuditRecords::getDeleteFlag, Boolean.FALSE)
                    //查询一条即可
                    .last("limit 1")
            );
            //判断是否存在bo
            if (Objects.nonNull(bo)){
                //返回结果
                return AjaxResult.error("您已完成过该用户发布的任务,无法再次完成。");
            }
        }
        //判断是否任务为人工审核
        AddWechat addWechat = addWechatService.getOne(new LambdaQueryWrapper<AddWechat>()
                //微信任务编号
                .eq(AddWechat::getId, auditRecords.getAddWechatId())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag, Boolean.FALSE)
                //类型
                .eq(AddWechat::getExamineType, AddWechatEnum.RenGong.getStatus())
        );
        //判断是否为 null
        if (Objects.isNull(addWechat)){
            //返回结果
            return AjaxResult.error("任务已下架或为暗号审核提交失败");
        }
        //判断该任务是否还需要人添加
        if (addWechat.getNeedCount() == 0){
            //返回提示
            return AjaxResult.error("该任务已经结束");
        }
        //判断审核图片
        if (StringUtils.isBlank(auditRecords.getAuditImg())){
            //返回结果
            return AjaxResult.error("请传递审核图片");
        }
        //判断审核微信号
        if (StringUtils.isBlank(auditRecords.getWechatNumber())){
            //返回结果
            return AjaxResult.error("请输入微信号");
        }
        //查询审核
        AuditRecords actived = auditRecordsService.getOne(new LambdaQueryWrapper<AuditRecords>()
                //任务id
                .eq(AuditRecords::getAddWechatId, auditRecords.getAddWechatId())
                //上传任务用户id
                .eq(AuditRecords::getUserId, SecurityUtils.getUserId())
                //查询一条
                .last("limit 1")
        );
        //判断是否不为 null
        if (Objects.nonNull(actived)){
            //返回提示
            return AjaxResult.error("用户已提交审核。");
        }
        auditRecords.setAuditType(AuditRecordsEnum.RenGong.getStatus());
        auditRecords.setAddStatus(AuditRecordsEnum.DaiShenHe.getStatus());
        //获取用户
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        //设置用户id
        auditRecords.setUserId(sysUser.getUserId());
        //设置默认值
        auditRecords.setCreateBy(SecurityUtils.getUsername());
        auditRecords.setCreateTime(new Date());
        auditRecords.setUpdateBy(SecurityUtils.getUsername());
        auditRecords.setUpdateTime(new Date());
        auditRecords.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        auditRecordsService.save(auditRecords);
        //用户任务需求人数减少 1
        addWechatService.update(null,new UpdateWrapper<AddWechat>().lambda()
                //任务编号id
                .eq(AddWechat::getId,addWechat.getId())
                //设置需求人数减少1 影响力 + 1
                .setSql("need_count = need_count - 1,influence = influence + 1")
        );
        //减少冻结表金额
        freezeService.update(null,new UpdateWrapper<Freeze>().lambda()
                //任务编号
                .eq(Freeze::getAddId,addWechat.getId())
                //设置金额 -1.5
                .setSql("freeze_fund = freeze_fund -1.5")
        );
        //返回结果
        return AjaxResult.success("审核提交成功");
    }

    @Resource
    private FreezeService freezeService;

    @Resource
    private AddWechatService addWechatService;

    //查出用户所有审核
    @PostMapping("/select/AuditRecords/getShengheData")
    public AjaxResult selectAuditUserUniapp(@RequestBody AuditRecords auditRecords){

        //创建分页对象
        Page<AuditRecords> page = new Page<>();
        //设置页码
        page.setCurrent(auditRecords.getCurrent());
        //设置行数
        page.setSize(auditRecords.getSize());


        //分页查询
        auditRecordsService.page(page,new LambdaQueryWrapper<AuditRecords>()
                //用户编号
                .eq(AuditRecords::getAddWechatUserId,SecurityUtils.getUserId())
                //审核状态
                .eq(AuditRecords::getAddStatus, AuditRecordsEnum.DaiShenHe.getStatus())
                //逻辑删除
                .eq(AuditRecords::getDeleteFlag,Boolean.FALSE)
                //通过创建时间排序
                .orderByAsc(AuditRecords::getCreateTime)
                //分页
                .last(" limit " + (auditRecords.getCurrent() - 1) * auditRecords.getSize() +"," + auditRecords.getSize())
        );
        page.setTotal(auditRecordsService.count(new LambdaQueryWrapper<AuditRecords>()
                //用户编号
                .eq(AuditRecords::getAddWechatUserId,SecurityUtils.getUserId())
                //审核状态
                .eq(AuditRecords::getAddStatus, AuditRecordsEnum.DaiShenHe.getStatus())
                //逻辑删除
                .eq(AuditRecords::getDeleteFlag,Boolean.FALSE)
        ));
        //遍历查询数据
        page.getRecords().stream().forEach(ele->{

            //判断是否有任务id
            if (Objects.nonNull(ele.getAddWechatId())){
                //查询任务
                AddWechat addWechat = addWechatService.getById(ele.getAddWechatId());
                //判断查询对象不为 null
                if (Objects.nonNull(addWechat)){
                    //设置对象给遍历对象
                    ele.setAddWechat(addWechat);
                }
            }
            //判断是否有用户id
            if (Objects.nonNull(ele.getUserId())){

                //查询用户信息
                SysUser sysUser = sysUserMapper.selectUserById(ele.getUserId());

                //判断查询对象不为 null
                if (Objects.nonNull(sysUser)){
                    //设置对象给遍历对象
                    ele.setUserXinXi(sysUser);
                }
                System.out.println(333);
                System.out.println(ele);
            }
        });
        //返回结果
        return AjaxResult.success(page);
    }
}
